package com.weirong.smartvault.dao;

import com.weirong.smartvault.entity.Authority;
import com.weirong.smartvault.entity.Role;
import com.weirong.smartvault.entity.UserRole;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Qualifier("userRoleRepository")
public interface UserRoleRepository extends JpaRepository<UserRole, Integer>{

    public List<UserRole> findUserRolesByUserId(int userId);

    @Query("SELECT role FROM Role role JOIN role.userRoles userRoles WHERE userRoles.userId=:userId")
    public List<Role> findRoleByUserId(@Param("userId") int userId);

    @Query("SELECT authority FROM Authority authority JOIN authority.roles roles JOIN roles.userRoles userRoles WHERE userRoles.userId=:userId")
    public List<Authority> findAuthorityByUserId(@Param("userId") int userId);

    @Query("SELECT userRole FROM UserRole userRole where userRole.userId = ?1")
    public List<UserRole> findByUserIdAndSort(int userId, Sort sort);

}
